﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTD2013.Entities;
using DevExpress.XtraTreeList;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using DTD2013.DTDLib;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using DTD2013.Common;

namespace DTD2013.MainApp.UI
{
    public partial class guiTableOfContent : XtraForm
    {
        #region Properties

        /// <summary>
        /// Gets or sets selected "CongTac" list
        /// </summary>
        public List<CongTac> SelectedList { get; set; }

        #endregion

        #region Variable

        /// <summary>
        /// "CongTac" list that binding to dgcCongViec grid control
        /// </summary>
        private List<CongTac> CongTacList = null;
        /// <summary>
        /// Data context entities
        /// </summary>
        private DeluxeEntities DeluxeContext = new DeluxeEntities();

        private string Province = string.Empty;
        private string CalculateMethod = string.Empty;

        #endregion

        public guiTableOfContent()
        {
            InitializeComponent();

            dgcCongViec.InitializeControl();
            dgcHaoPhi.InitializeControl();

            SelectedList = new List<CongTac>();
            
            CongTacList = new List<CongTac>();
            dgcCongViec.DataSource = CongTacList;

            // event handler
            txtSearchContent.EditValueChanging += new ChangingEventHandler(txtSearchContent_EditValueChanging);
            //dgvCongViec.RowClick += new RowClickEventHandler(dgvCongViec_RowClick);
            trlTableOfContent.FocusedNodeChanged += new FocusedNodeChangedEventHandler(trlTableOfContent_FocusedNodeChanged);
            trlTableOfContent.MouseClick += new MouseEventHandler(trlTableOfContent_MouseClick);
            dgvCongViec.MouseDown += new MouseEventHandler(dgvCongViec_MouseDown);
            dgvCongViec.CustomDrawCell += new RowCellCustomDrawEventHandler(dgvCongViec_CustomDrawCell);
        }

        private void dgvCongViec_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            CongTac cv = dgvCongViec.GetRow(e.RowHandle) as CongTac;
            if (cv != null)
            {
                if (cv.Chon.Value)
                {
                    e.Appearance.ForeColor = Color.Red;
                }
                if (CalculateMethod == Constant.DEFAULT_NORM && cv.DGVL + cv.DGNC + cv.DGCM == 0)
                {
                    e.Appearance.ForeColor = Color.Gray;
                }
            }
        }

        private void dgvCongViec_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Location);
            if (hitInfo.Column != null && hitInfo.Column.FieldName == "Chon" && hitInfo.RowHandle >= 0)
            {
                CongTac cv = view.GetRow(hitInfo.RowHandle) as CongTac;
                if (CalculateMethod == Constant.DEFAULT_NORM && cv.DGVL.Value + cv.DGNC.Value + cv.DGCM.Value == 0)
                {
                    cv.Chon = false;
                }
                else
                {
                    cv.Chon = !cv.Chon;
                }
                dgcHaoPhi.DataSource = DataUtilities.GetWasteOfWork(cv.MaHieu, Province);
                dgcCongViec.RefreshDataSource();
            }
        }

        public guiTableOfContent(string maHieu, string province, string calculateMethod) : this()
        {
            txtSearchContent.Text = maHieu;
            Province = province;
            CalculateMethod = calculateMethod;
        }
        
        private void guiTableOfContent_Load(object sender, EventArgs e)
        {
            trlTableOfContent.DataSource = DeluxeContext.MucLucs;
            txtSearchContent_EditValueChanging(null, null);
        }

        private void txtSearchContent_EditValueChanging(object sender, ChangingEventArgs e)
        {
            string maHieu = txtSearchContent.Text;
            List<CongTac> congTacs = DeluxeContext.CongTacs.Where(c => c.MaHieu.StartsWith(maHieu)).ToList();
            CongTacList.Clear();
            CongTacList.AddRange(congTacs);
            dgcCongViec.RefreshDataSource();
        }

        private void trlTableOfContent_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            string dk1 = e.Node["DieuKien1"].ToString();
            string dk2 = e.Node["DieuKien2"].ToString();
            string dk3 = e.Node["DieuKien3"].ToString();
            string dk4 = e.Node["DieuKien4"].ToString();
            string dk5 = e.Node["DieuKien5"].ToString();
            string dk6 = e.Node["DieuKien6"].ToString();
            string dk7 = e.Node["DieuKien7"].ToString();
            string dk8 = e.Node["DieuKien8"].ToString();
            string dk9 = e.Node["DieuKien9"].ToString();
            string bdm = e.Node["BoDinhMuc"].ToString();

            CongTacList.Clear();
            // Lay bo dinh muc
            List<CongTac> bdmList = DeluxeContext.CongTacs.Where(c => c.NhomDM.Equals(bdm)).ToList();
            // lay dk1
            if (!string.IsNullOrEmpty(dk1))
            {
                List<CongTac> dk1List = bdmList.Where(c => c.MaHieu.StartsWith(dk1)).ToList();
                CongTacList.AddRange(dk1List);
            }
            // lay dk2
            if (!string.IsNullOrEmpty(dk2))
            {
                List<CongTac> dk2List = bdmList.Where(c => c.MaHieu.StartsWith(dk2)).ToList();
                CongTacList.AddRange(dk2List);
            }
            // lay dk3
            if (!string.IsNullOrEmpty(dk3))
            {
                List<CongTac> dk3List = bdmList.Where(c => c.MaHieu.StartsWith(dk3)).ToList();
                CongTacList.AddRange(dk3List);
            }
            // lay dk4
            if (!string.IsNullOrEmpty(dk4))
            {
                List<CongTac> dk4List = bdmList.Where(c => c.MaHieu.StartsWith(dk4)).ToList();
                CongTacList.AddRange(dk4List);
            }
            // lay dk5
            if (!string.IsNullOrEmpty(dk5))
            {
                List<CongTac> dk5List = bdmList.Where(c => c.MaHieu.StartsWith(dk5)).ToList();
                CongTacList.AddRange(dk5List);
            }
            // lay dk6
            if (!string.IsNullOrEmpty(dk6))
            {
                List<CongTac> dk6List = bdmList.Where(c => c.MaHieu.StartsWith(dk6)).ToList();
                CongTacList.AddRange(dk6List);
            }
            // lay dk7
            if (!string.IsNullOrEmpty(dk7))
            {
                List<CongTac> dk7List = bdmList.Where(c => c.MaHieu.StartsWith(dk7)).ToList();
                CongTacList.AddRange(dk7List);
            }
            // lay dk8
            if (!string.IsNullOrEmpty(dk8))
            {
                List<CongTac> dk8List = bdmList.Where(c => c.MaHieu.StartsWith(dk8)).ToList();
                CongTacList.AddRange(dk8List);
            }
            // lay dk9
            if (!string.IsNullOrEmpty(dk9))
            {
                List<CongTac> dk9List = bdmList.Where(c => c.MaHieu.StartsWith(dk9)).ToList();
                CongTacList.AddRange(dk9List);
            }
            if (CongTacList.Count == 0)
            {
                CongTacList.AddRange(bdmList);
            }

            dgcCongViec.RefreshDataSource();
        }

        private void trlTableOfContent_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu menu = new ContextMenu();
                menu.MenuItems.Add("Mở tất cả", new EventHandler(ExpandAll_Clicked));
                menu.MenuItems.Add("Đóng tất cả", new EventHandler(CollapseAll_Clicked));
                menu.Show(trlTableOfContent, new Point(e.X, e.Y));
            }
        }

        private void ExpandAll_Clicked(object sender, EventArgs e)
        {
            trlTableOfContent.ExpandAll();
        }

        private void CollapseAll_Clicked(object sender, EventArgs e)
        {
            trlTableOfContent.CollapseAll();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SelectedList.Clear();
            List<CongTac> selectedList = CongTacList.Where(c => c.Chon.Value).ToList();
            foreach (CongTac cv in selectedList)
            {
                CongTac tmp = new CongTac();
                PropertiesMapper.CopyPropertiesFrom(tmp, cv);
                SelectedList.Add(tmp);
            }
            DialogResult = DialogResult.OK;
        }
    }
}
